package handler

import (
	"github.com/starmotioncf/soutchatinternal/dto"
	"github.com/starmotioncf/soutchatinternal/errcode"
	"github.com/starmotioncf/soutchatinternal/resp"
	"github.com/starmotioncf/soutchatinternal/service"

	"github.com/gin-gonic/gin"
	"github.com/opdss/version"
)

type CommonHandler struct {
	commonService service.CommonService
}

func NewIndexHandler(commonService service.CommonService) *CommonHandler {
	return &CommonHandler{
		commonService: commonService,
	}
}

func (h *CommonHandler) Index(ctx *gin.Context) {
	resp.Success(ctx, version.Build)
}

func (h *CommonHandler) Version(ctx *gin.Context) {
	resp.Success(ctx, version.Build)
}

// GetPresignURL 获取上传地址
// @Tags 公共接口
// @Summary  获取上传地址
// @Description 获取上传地址
// @Accept json
// @param form query dto.PresignUrlReq true "参数"
// @Produce  json
// @Response 200 {object} resp.Resp{data=dto.PresignUrlRes}
// @Router /presign/url [get]
func (h *CommonHandler) GetPresignURL(ctx *gin.Context) {
	var req dto.PresignUrlReq
	if err := ctx.ShouldBind(&req); err != nil {
		resp.Error(ctx, errcode.ErrInvalidParams.Wrap(err))
		return
	}
	url, err := h.commonService.GetPresignURL(ctx, &req)
	if err != nil {
		resp.Error(ctx, errcode.ErrUnknown.Wrap(err))
		return
	}
	resp.ReturnMustJson(ctx, dto.PresignUrlRes{url}, err)
}
